package com.cloud.lawcase.api.mapper;

import java.util.List;

import com.cloud.lawcase.api.bean.LawItemSearchCriteria;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import com.cloud.lawcase.api.bean.LawCauseSearchCriteria;
import com.cloud.lawcase.api.entity.TcDicLawItem;
import com.cloud.lawcase.api.provider.TcDicLawItemPageProvider;
import com.cloud.lawcase.common.driver.SimpleInlangDriver;
import com.cloud.lawcase.common.openapi.OpenApiLawCauseItemBean;
import com.cloud.lawcase.common.utils.BaseMapperHelper;

public interface TcDicLawItemMapper extends BaseMapperHelper<TcDicLawItem> {

	@Delete("delete from tc_dic_law_item where law_id in (select law_id from tc_dic_law where group_id = #{group_id})")
	void deleteByGroupId(Long groupId);

	@SelectProvider(type = TcDicLawItemPageProvider.class, method = "listPage")
	@Results({ @Result(property = "lawId", column = "law_id", javaType = Long.class),
			@Result(property = "itemId", column = "item_id", javaType = Long.class),
			@Result(property = "lawTitle", column = "law_title", javaType = String.class),
			@Result(property = "itemCode", column = "item_code", javaType = String.class),
			@Result(property = "itemName", column = "item_name", javaType = String.class),
			@Result(property = "itemContent", column = "item_content", javaType = String.class) })
	List<TcDicLawItem> listLawItemPage(LawCauseSearchCriteria criteria);


	@SelectProvider(type = TcDicLawItemPageProvider.class, method = "list")
	@Results({ @Result(property = "lawId", column = "law_id", javaType = Long.class),
			@Result(property = "itemId", column = "item_id", javaType = Long.class),
			@Result(property = "lawTitle", column = "law_title", javaType = String.class),
			@Result(property = "itemName", column = "item_name", javaType = String.class),
			@Result(property = "itemContent", column = "item_content", javaType = String.class) })
	List<TcDicLawItem> listLawItem(LawItemSearchCriteria criteria);

	@Select("select t1.law_id, t1.law_title, t2.item_id, t2.item_name, t2.item_content,t3.item_type_id from tc_dic_law t1, tc_dic_law_item t2,tc_law_rel_cause_law_item t3  where t1.law_id = t2.law_id and t2.item_id = t3.item_id and t2.item_id in (#{ids}) and t3.cause_id = ${causeId}")
	@Lang(SimpleInlangDriver.class)
	@Results({ @Result(property = "lawId", column = "law_id", javaType = Long.class),
			@Result(property = "itemId", column = "item_id", javaType = Long.class),
			@Result(property = "lawTitle", column = "law_title", javaType = String.class),
			@Result(property = "itemName", column = "item_name", javaType = String.class),
			@Result(property = "itemContent", column = "item_content", javaType = String.class),
			@Result(property = "itemTypeId", column = "item_type_id", javaType = Integer.class) })
	List<TcDicLawItem> listInfringeItems(@Param("ids") List<Long> ids, @Param("causeId") Long causeId);

	@Select("select t3.law_code, t3.law_title, t2.item_code, t2.item_name, t2.item_content, t1.item_type_id, t1.cause_id from tc_law_rel_cause_law_item t1, tc_dic_law_item t2, tc_dic_law t3 where t1.item_id = t2.item_id and t1.law_id = t3.law_id and t1.cause_id in (#{ids})")
	@Lang(SimpleInlangDriver.class)
	@Results({ @Result(property = "lawCode", column = "law_code", javaType = String.class),
			@Result(property = "lawTitle", column = "law_title", javaType = String.class),
			@Result(property = "itemName", column = "item_name", javaType = String.class),
			@Result(property = "itemContent", column = "item_content", javaType = String.class),
			@Result(property = "itemCode", column = "item_code", javaType = String.class),
			@Result(property = "causeId", column = "cause_id", javaType = Long.class),
			@Result(property = "itemTypeId", column = "item_type_id", javaType = Integer.class) })
	List<OpenApiLawCauseItemBean> listCausesItems(@Param("ids") List<Long> causeIds);

	@Select("select t2.law_code, t2.law_title, t1.item_code, t1.item_name, t1.item_id, t1.item_content from tc_dic_law_item t1, tc_dic_law t2 where t1.law_id = t2.law_id and t1.item_id in(#{ids})")
	@Lang(SimpleInlangDriver.class)
	@Results({ @Result(property = "lawCode", column = "law_code", javaType = String.class),
			@Result(property = "lawTitle", column = "law_title", javaType = String.class),
			@Result(property = "itemName", column = "item_name", javaType = String.class),
			@Result(property = "itemContent", column = "item_content", javaType = String.class),
			@Result(property = "itemCode", column = "item_code", javaType = String.class),
			@Result(property = "itemId", column = "item_id", javaType = Long.class),
			@Result(property = "itemTypeId", column = "item_type_id", javaType = Integer.class) })
	List<OpenApiLawCauseItemBean> listPunishItems(@Param("ids") List<Long> causeItemIds);

	@Select("select item_id from tc_dic_law_item where law_id = #{lawId} "
			+ "and article_id = #{articleId} and paragraph_id = #{paragraphId} "
			+ "and sub_paragraph_id = #{subParagraphId} and entry_id = #{entryId}")
	@ResultType(Long.class)
	List<Long> queryItemIdByLawAndChapter(TcDicLawItem item);
	
	@Select("select t1.cause_id, t2.item_code, t2.item_content, t2.item_name from tc_dic_law_cause t1, tc_dic_law_item t2 where t1.item_id = t2.item_id and t1.cause_id in (#{ids})")
	@Lang(SimpleInlangDriver.class)
	@Results({ 
		@Result(property = "causeId", column = "cause_id", javaType = Long.class),
		@Result(property = "itemCode", column = "item_code", javaType = String.class),
		@Result(property = "itemContent", column = "item_content", javaType = String.class),
		@Result(property = "itemName", column = "item_name", javaType = String.class) })
	List<TcDicLawItem> listPunishItemByCauseIds(@Param("ids") List<Long> causeIds);
	
	@Select("select t1.cause_id, t3.item_code, t3.item_content, t3.item_name from tc_dic_law_cause t1, tc_law_rel_cause_law_item t2, tc_dic_law_item t3 where t1.cause_id = t2.cause_id and t2.item_id = t3.item_id and t2.item_type_id = 1 and t1.cause_id in (#{ids})")
	@Lang(SimpleInlangDriver.class)
	@Results({ 
		@Result(property = "causeId", column = "cause_id", javaType = Long.class),
		@Result(property = "itemCode", column = "item_code", javaType = String.class),
		@Result(property = "itemContent", column = "item_content", javaType = String.class) ,
			@Result(property = "itemName", column = "item_name", javaType = String.class)})
	List<TcDicLawItem> listInfringeItemByCauseIds(@Param("ids") List<Long> causeIds);
}
